//
//  YMJflowlayout.m
//  QQMV
//
//  Created by yh on 16/8/13.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "YMJflowlayout.h"
#define CELLHEIGHT 145
#define CELLWIDTH 145
#define LINESPACING 10
#define INTERITEMSPACING 10
#define SUPPERVIEWHEIGHTSPACING 300
#define NAMEHEIGHT 50
@interface YMJflowlayout ()
@property (nonatomic, strong) NSMutableArray *array_att;
@property (nonatomic, assign) CGFloat y_sup;
@property (nonatomic, assign) CGFloat height_MAX;
@end
@implementation YMJflowlayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _array_att = [NSMutableArray array];
        _y_sup = 0;
        _height_MAX = 0;
    }
    return self;
}
- (void)prepareLayout {
    [super prepareLayout];
    NSMutableArray *arr_lay = [NSMutableArray arrayWithObjects:@(0 + SUPPERVIEWHEIGHTSPACING), @(1 + SUPPERVIEWHEIGHTSPACING), nil];
    NSInteger minIdx = 0;
    for (int j = 0; j < [self.collectionView numberOfSections]; j++) {
        for (int i = 0; i < [self.collectionView numberOfItemsInSection:j]; i++) {
            if (i == 0) {
                NSIndexPath *indePath = [NSIndexPath indexPathForRow:i inSection:j];
                UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indePath];
                CGFloat x_sup = i * LINESPACING + LINESPACING;
                _y_sup = [arr_lay[minIdx] floatValue] - SUPPERVIEWHEIGHTSPACING; // 竖着走
                CGFloat width_sup = SUPPERVIEWHEIGHTSPACING;
                CGFloat height_sup = SUPPERVIEWHEIGHTSPACING;
                att.frame = CGRectMake(x_sup, _y_sup, width_sup, height_sup);
                [_array_att addObject:att];
            } else {
                NSIndexPath *indePath = [NSIndexPath indexPathForRow:i inSection:j];
                UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indePath];
                CGFloat x = minIdx * (CELLWIDTH + INTERITEMSPACING) + INTERITEMSPACING;
                CGFloat y = [arr_lay[minIdx] floatValue] + LINESPACING;
                CGFloat width = CELLWIDTH;
                CGFloat height = CELLHEIGHT;
                att.frame = CGRectMake(x, y, width, height);
                [_array_att addObject:att];
                arr_lay[minIdx] = @(y + CELLHEIGHT) ;
                for (int i = 0; i < arr_lay.count; i++) {
                    if (arr_lay[i] < arr_lay[minIdx]) {
                        minIdx = i;
                    }
                }
                
            }
        }
        // 每次执行完一轮回
        // 求出最大高度
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
        [arr_lay sortUsingDescriptors:@[sort]];
        _height_MAX = [[arr_lay lastObject] floatValue];
        arr_lay = [NSMutableArray arrayWithObjects:@(0 + SUPPERVIEWHEIGHTSPACING + _height_MAX), @(1 + SUPPERVIEWHEIGHTSPACING + _height_MAX), nil];
    }
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0, _height_MAX);
}
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    for (UICollectionViewLayoutAttributes *att in _array_att) {
        NSLog(@"去哦发货时发布%@",NSStringFromCGRect(att.frame));
    }
    return _array_att;
}
@end
